package com.hngy.xpq.android_practice.ui.notifications;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.hngy.xpq.android_practice.MainActivity;
import com.hngy.xpq.android_practice.OrderActivity;
import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.assets.XpqIcoMoon;
import com.hngy.xpq.android_practice.assets.XpqIcoMoonUtils;
import com.hngy.xpq.android_practice.bean.Recording;

import static android.content.Context.MODE_PRIVATE;

public class NotificationsFragment extends Fragment {

    SharedPreferences main,comm,history;       //节约流量的概念存储
    Button favorites,recording,wallet,order,tuichu;    //功能
    ImageButton avatar;    //头像更换
    TextView name,f,r,w,frag;

    MainActivity activity = (MainActivity) getActivity();

    public View onCreateView(@NonNull final LayoutInflater inflater,
                             final ViewGroup container, final Bundle savedInstanceState) {
        NotificationUser();
        final View root = inflater.inflate(R.layout.fragment_notifications, container, false);

        tuichu = root.findViewById(R.id.tuichu);
        favorites = root.findViewById(R.id.XPQ_FAVORITES);
        recording = root.findViewById(R.id.XPQ_RECORDING);
        wallet = root.findViewById(R.id.XPQ_WALLET);
        order = root.findViewById(R.id.XPQ_ORDER);
        frag = root.findViewById(R.id.frag);
        avatar = root.findViewById(R.id.fragment_avatar);

        //用户信息
        name = root.findViewById(R.id.wd_name);
        if (!main.getString("name","").equals("")){
            name.setText(main.getString("name",""));
        } else {
            tuichu.setText("登陆");
        }

        avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    //跳转头像修改页面
                Intent intent = new Intent(getActivity(),AvatarActivity.class);
                startActivityForResult(intent,0x13);
            }
        });
        tuichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {      //退出登陆
                Intent intent = new Intent(getActivity(), OrderActivity.class);
                //清理内存缓存文档，给新账号腾运行地方
                SharedPreferences.Editor editor = comm.edit();
                editor.putString("name","");
                editor.putString("phone","");
                editor.putString("price","");
                editor.putString("comm_name","");
                editor.putString("like","");
                editor.commit();
                SharedPreferences.Editor editor1 = history.edit();
                editor1.putString("history","");
                editor1.commit();
                SharedPreferences.Editor editor2 = main.edit();
                editor2.putString("name","");
                editor2.putString("key","");
                editor2.putString("check","");
                editor2.commit();
                startActivity(intent);
            }
        });
        wallet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {      //钱包
                if (main.getString("name","").equals("") ){
                    Intent intent = new Intent(getActivity(),OrderActivity.class);
                    Toast.makeText(getActivity(),"请登陆!!",Toast.LENGTH_SHORT);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity(),WalletActivity.class);
                    startActivity(intent);
                }
            }
        });
        favorites.setOnClickListener(new View.OnClickListener() { //收藏
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), RecordingComm.class);
                SharedPreferences.Editor editor = comm.edit();
                editor.putString("like","");
                editor.commit();
                startActivity(intent);
            }
        });
        order.setOnClickListener(new View.OnClickListener() {      //订单
            @Override
            public void onClick(View v) {     //订单状态
                Intent intent = new Intent(getActivity(), NotificatiosOrder.class);
                startActivity(intent);
            }
        });
        recording.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    //浏览痕迹
                Intent intent = new Intent(getActivity(),RecordingComm.class);
                SharedPreferences.Editor editor = comm.edit();
                editor.putString("like","recording");
                editor.commit();
                startActivity(intent);
            }
        });
        IcoMoonUtil();
        return root;
    }

    private void IcoMoonUtil() {       //四大功能的icomoon文字图标的显示
        favorites.setText(XpqIcoMoon.FAVORITES);
        recording.setText(XpqIcoMoon.RECORDING);
        wallet.setText(XpqIcoMoon.WALLET);
        order.setText(XpqIcoMoon.ORDER);
        frag.setText(XpqIcoMoon.SET);
        XpqIcoMoonUtils.setIOC(favorites,recording,wallet,order,frag);
    }

    private void NotificationUser() {            //调用存储
        main = getActivity().getSharedPreferences("main",MODE_PRIVATE);
        comm = getActivity().getSharedPreferences("comm",MODE_PRIVATE);
        history = getActivity().getSharedPreferences("history",MODE_PRIVATE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 0x15){
            Bundle bundle = data.getExtras();
            int icon=bundle.getInt("icon");
            ImageView iv= getActivity().findViewById(R.id.fragment_tx);
            iv.setImageResource(icon);
        }
    }
}